package com.jwgf.Half485.modbusthreadpool;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.Calendar;

public class ModbusCommandFeeder {
    public ModbusCommandFeeder() {

    }

    @Subscribe
    public void onHoldingRegister(PLCData.ModbusResult result) {
        if (result.addr == 698) {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            ModbusCommandManager.getInstance().putNormalCmd(getPollingCmd());
        }
    }

    public void start() {
        EventBus.getDefault().register(this);
        ModbusCommandManager.getInstance().putPriorityCmd(getInitCmd());
        ModbusCommandManager.getInstance().putNormalCmd(getPollingCmd());
    }

    private ArrayList<byte[]> getInitCmd() {
        ArrayList<byte[]> cmdList = new ArrayList<byte[]>();
        cmdList.add(ModbusUtils.createReadHoldingRegisterCmd((byte) 1, 0x0, 9));
        cmdList.add(ModbusUtils.createReadHoldingRegisterCmd((byte) 1, 0x9, 1));
        cmdList.add(ModbusUtils.createReadHoldingRegisterCmd((byte) 1, 0x0C, 3));
        cmdList.add(ModbusUtils.createReadHoldingRegisterCmd((byte) 1, 0x32, 15));
        cmdList.add(ModbusUtils.createReadHoldingRegisterCmd((byte) 1, 0x64, 4));

        // PLC版本
        cmdList.add(ModbusUtils.createReadHoldingRegisterCmd((byte) 1, 0xF9, 3));//机头版本号
        cmdList.add(ModbusUtils.createReadHoldingRegisterCmd((byte) 1, 0x131, 2));//CBF细络联版本号
        cmdList.add(ModbusUtils.createReadHoldingRegisterCmd((byte) 1, 0x13E, 1));//CBF细络联版本号
        cmdList.add(ModbusUtils.createReadHoldingRegisterCmd((byte) 1, 0x15E, 2));//大纱库版本号
        cmdList.add(ModbusUtils.createReadHoldingRegisterCmd((byte) 1, 0x162, 1));//大纱库版本号
        return cmdList;
    }

    private ArrayList<byte[]> getPollingCmd() {
        ArrayList<byte[]> cmdList = new ArrayList<byte[]>();

        cmdList.add(ModbusUtils.createReadHoldingRegisterCmd((byte) 1, 0xFC, 3)); //504-508
        cmdList.add(ModbusUtils.createReadHoldingRegisterCmd((byte) 1, 0x12C, 8));//600/2
        cmdList.add(ModbusUtils.createReadHoldingRegisterCmd((byte) 1, 0x140, 14));//640/2
        cmdList.add(ModbusUtils.createReadHoldingRegisterCmd((byte) 1, 0x15E, 3));//700/2
        cmdList.add(ModbusUtils.createReadHoldingRegisterCmd((byte) 1, 307, 1));//细络联报警
        cmdList.add(ModbusUtils.createReadHoldingRegisterCmd((byte) 1, 309, 9));//RFID，细络联二通道,RFID时间
        cmdList.add(ModbusUtils.createReadHoldingRegisterCmd((byte) 1, 332, 1));//CBF报警
        cmdList.add(ModbusUtils.createReadHoldingRegisterCmd((byte) 1, 333, 1));//锭节启动
        cmdList.add(ModbusUtils.createReadHoldingRegisterCmd((byte) 1, 334, 1));//空管通道满信号668.6
        cmdList.add(ModbusUtils.createReadHoldingRegisterCmd((byte) 1, 70, 1));//换班清洁报警
        cmdList.add(ModbusUtils.createReadHoldingRegisterCmd((byte) 1, 245, 3));//电压，电流，温度
        cmdList.add(ModbusUtils.createReadHoldingRegisterCmd((byte) 1, 0xA, 1)); //筒管库


        //系统时间同步
        Calendar c = Calendar.getInstance();
        int second = c.get(Calendar.SECOND);
        int minute = c.get(Calendar.MINUTE);
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int day = c.get(Calendar.DAY_OF_MONTH);
        int month = c.get(Calendar.MONTH) + 1;
        int year = c.get(Calendar.YEAR);
        byte[] cmd = new byte[12];
        cmd[0] = (byte) ((second >> 8) & 0xff);
        cmd[1] = (byte) (second & 0xff);
        cmd[2] = (byte) ((minute >> 8) & 0xff);
        cmd[3] = (byte) (minute & 0xff);
        cmd[4] = (byte) ((hour >> 8) & 0xff);
        cmd[5] = (byte) (hour & 0xff);
        cmd[6] = (byte) ((day >> 8) & 0xff);
        cmd[7] = (byte) (day & 0xff);
        cmd[8] = (byte) ((month >> 8) & 0xff);
        cmd[9] = (byte) (month & 0xff);
        cmd[10] = (byte) ((year >> 8) & 0xff);
        cmd[11] = (byte) (year & 0xff);
        cmdList.add(ModbusUtils.createWriteRegisterCmd((byte) 1, 680, cmd));
        // 加密功能
        cmdList.add(ModbusUtils.createReadHoldingRegisterCmd((byte) 1, 2500, 1));//bit1是否加密,bit3是否弹框
        cmdList.add(ModbusUtils.createReadHoldingRegisterCmd((byte) 1, 698, 6));//PLC_ID
        return cmdList;
    }
}
